﻿/* 
 * CIFSControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to CIFS tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class CIFSControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Validate whether entered data is in proper format or not
            if (validatedata() == false)
                return;

            // Netbiosname
            if ("AFS".Equals(netbiosNameTextBox.Text))
            {
                serviceParameters.DeleteValue("NetbiosName", false);
            }
            else
            {
                if ((netbiosNameTextBox.Text[0] >= 65 && netbiosNameTextBox.Text[0] <= 90) || (netbiosNameTextBox.Text[0] >= 97 && netbiosNameTextBox.Text[0] <= 122))
                {
                    serviceParameters.SetValue("NetbiosName", netbiosNameTextBox.Text, RegistryValueKind.ExpandString);
                }
                else
                {
                    //error name should start with an alphabet
                }
            }

            // Locate LAN Adapter
            if (locateAdapterCheckBox.Checked)
            {
                serviceParameters.SetValue("NoFindLanaByName", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("NoFindLanaByName", false);
            }

            // Multiplexed Requests
            if ("50".Equals(multiplexedRequestsTextBox.Text))
            {
                serviceParameters.DeleteValue("MaxMpxRequests", false);
            }
            else
            {
                if (checkint(multiplexedRequestsTextBox.Text))
                {
                    serviceParameters.SetValue("MaxMpxRequests", Int32.Parse(multiplexedRequestsTextBox.Text), RegistryValueKind.DWord);
                }
                else 
                {
                    //error nume should be an integer
                }
            }

            // SMB Virtual Circuits
            if ("100".Equals(maximumVirtualCircuitsTextBox.Text))
            {
                serviceParameters.DeleteValue("MaxVCPerServer", false);
            }
            else
            {
                if (checkint(maximumVirtualCircuitsTextBox.Text))
                {
                    serviceParameters.SetValue("MaxVCPerServer", Int32.Parse(maximumVirtualCircuitsTextBox.Text), RegistryValueKind.DWord);
                }
                else 
                {
                    //error nume should be an integer
                }
            }

            // SMB Authentication Level
            if (2 == authenticationLevelComboBox.SelectedIndex)
            {
                serviceParameters.DeleteValue("smbAuthType", false);
            }
            else
            {
                serviceParameters.SetValue("smbAuthType", authenticationLevelComboBox.SelectedIndex, RegistryValueKind.DWord);
            }

            //Enable Submount
            if (subMountCheckBox.Checked)
            {
                serviceParameters.SetValue("AllSubmount", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("AllSubmount", false);
            }
        }
        catch (SecurityException)
        {
            MessageBox.Show("You do not have access to apply these settings.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    // Cancel the operations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();

    }

    // CSC (Client Side Caching) Policy

    // Add/Modify share name and policy
    private void buttonAddModify_Click(object sender, EventArgs e)
    {
        if (sharenameTextBox.Text.Equals(""))
        {
            String error = "Share name should be a non-empty string";
            MessageBox.Show(error);
            return;
        }
        if (policyComboBox.SelectedIndex == -1)
        {
            String error = "Policy should be one of the given values";
            MessageBox.Show(error);
            return;
        }

        shareNames.SetValue(sharenameTextBox.Text, policyComboBox.SelectedItem.ToString(), RegistryValueKind.String);
        
        shareNames = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\CSCPolicy", true);
        if (shareNames != null)
        {
            shareName = shareNames.GetValueNames();
            shareCount = shareNames.ValueCount;
        }
        datagrid.Rows.Clear();
        for (int i = 0; i < shareCount; i++) datagrid.Rows.Add(shareName.GetValue(i).ToString(), shareNames.GetValue(shareName.GetValue(i).ToString()));
    }

    // Remove share name and policy
    private void buttonRemove_Click(object sender, EventArgs e)
    {
        if (sharenameTextBox.Text.Equals(""))
        {
            String error = "Share name should be a non-empty string";
            MessageBox.Show(error);
            return;
        }

        shareNames = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\CSCPolicy", true);
        if (shareNames != null)
        {
            shareName = shareNames.GetValueNames();
            shareCount = shareNames.ValueCount;
        }

        int index = -1, i = 0;
        for (i = 0; i < shareCount; i++) if (sharenameTextBox.Text.Equals(shareName.GetValue(i).ToString())) index = i;
        if (index != -1)
        {
            if (policyComboBox.SelectedItem.ToString().Equals(shareNames.GetValue(sharenameTextBox.Text).ToString()))
            {
                shareNames.DeleteValue(sharenameTextBox.Text, false);
            }
        }
        shareNames = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\CSCPolicy", true);
        if (shareNames != null)
        {
            shareName = shareNames.GetValueNames();
            shareCount = shareNames.ValueCount;
        }
        datagrid.Rows.Clear();
        for (i = 0; i < shareCount; i++) datagrid.Rows.Add(shareName.GetValue(i).ToString(), shareNames.GetValue(shareName.GetValue(i).ToString()));
    }

    // Error checking
    private bool validatedata()
    {
        if (netbiosNameTextBox.Text.Equals(""))
        {
            String error = "NetBIOS name should be a non-empty string";
            MessageBox.Show(error);
            return false;
        }
        if (!checkint(multiplexedRequestsTextBox.Text))
        {
            String error = "Number of Multiplexed Requests should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if (!checkint(maximumVirtualCircuitsTextBox.Text))
        {
            String error = "Number of Maximum Virtual Circuits should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if (authenticationLevelComboBox.SelectedIndex == -1)
        {
            String error = "Authentication Level should be one of the given values";
            MessageBox.Show(error);
            return false;
        }
        return true;
    }
    
    private bool checkint(string a)
    {
        bool flag = true;

        if (a.Equals("")) flag = false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] < 48 || a[i] > 57) flag = false;
        }
        return flag;
    }
}